package com.rongshu.sendmessage.activity;

import android.net.http.SslError;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.webkit.SslErrorHandler;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.rongshu.sendmessage.R;
import com.rongshu.sendmessage.base.BaseActivity;
import com.rongshu.sendmessage.util.StringUtils;
import com.rongshu.sendmessage.util.httpUtil.LoadingView;
import com.rongshu.sendmessage.view.TitileLayout;


public class PlayVideoWebViewActivity extends BaseActivity {
    private WebView about_wv;
    private LoadingView loadingView;
    private boolean isHead;
    private TitileLayout titleView;
    private String webUrl;

    //private ImageView back;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_play_video_web_view);
        isHead=getIntent().getBooleanExtra("isHead",false);
        titleView = (TitileLayout) findViewById(R.id.titleView);
        String source = getIntent().getStringExtra("source");
        if ("help".equals(source)){
            titleView.setTitle("帮助中心");
        }else if ("xieyi".equals(source)){
            titleView.setTitle("用户协议");
        }else if ("yinsi".equals(source)){
            titleView.setTitle("隐私协议");
        }else if ("login".equals(source)){
            titleView.setTitle("软件更新");
        }else if ("message".equals(source)){
            titleView.setTitle("消息");
        }else{
            String title = getIntent().getStringExtra("title");
            if(!StringUtils.isEmpty(title)){
                titleView.setTitle(title);
            }else{
                titleView.setTitle("");
            }

        }
        webUrl=getIntent().getStringExtra("webUrl");
        titleView.hideRight();
        titleView.setTitleClickListener(new TitileLayout.TitleClickListener() {
            @Override
            public void onLeftClick() {
                finish();
            }

            @Override
            public void onRightClick() {

            }

            @Override
            public void onTitleClick() {

            }
        });
        loadingView = (LoadingView)findViewById(R.id.loading_v);
        about_wv = (WebView) findViewById(R.id.address_wv);
        about_wv.getSettings().setDefaultTextEncodingName("UTF-8") ;
        about_wv.setWebViewClient(new WebViewClient() {
            @Override
            public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
                // TODO Auto-generated method stub
                handler.proceed();// 接受所有网站的证书
            }
        });
        WebSettings webSettings = about_wv.getSettings();
        webSettings.setJavaScriptEnabled(true);//允许使用js

        webSettings.setDomStorageEnabled(true);

        webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);//不使用缓存，只从网络获取数据.
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            webSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        }
        about_wv.loadUrl(webUrl);
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        Log.i("ansen", "是否有上一个页面:" + about_wv.canGoBack());
        if (about_wv.canGoBack() && keyCode == KeyEvent.KEYCODE_BACK) {//点击返回按钮的时候判断有没有上一页
            about_wv.goBack(); // goBack()表示返回webView的上一页面
        }else if(!about_wv.canGoBack()){
            finish();
        }
        return true;
    }



    /**
     * Android 关闭WebView之后，声音或者视频不停止的解决办法
     * 清除webview释放内存防止oom
     */
    @Override
    protected void onDestroy() {
        super.onDestroy();
        about_wv.reload ();
        about_wv.removeAllViews();
        about_wv.destroy();
        finish();
    }

}


